/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCLine2D.h"

namespace FaceCat{
    FCLine2D::FCLine2D(){
        m_close = false;
        m_lineWidth = 1;
        m_points = L"0,0 100%,100%";
    }

    FCLine2D::~FCLine2D(){
    }

    bool FCLine2D::isClose(){
        return m_close;
    }

    void FCLine2D::setClose(bool value){
        m_close = value;
    }

    float FCLine2D::getLineWidth(){
        return m_lineWidth;
    }

    void FCLine2D::setLineWidth(float value){
        m_lineWidth = value;
    }

    String FCLine2D::getPoints(){
        return m_points;
    }

    void FCLine2D::setPoints(String value){
        m_points = value;
    }

    String FCLine2D::getViewType(){
        return L"FCLine2D";
    }

    void FCLine2D::getAttribute(const String& name, String *value, String *type){
        if (name == L"close"){
            *type = L"bool";
            *value = FCTran::boolToStr(isClose());
        }
        else if(name == L"linewidth"){
            *type = L"float";
            *value = FCTran::floatToStr(getLineWidth());
        }
        else if(name == L"points"){
            *type = L"point";
            *value = getPoints();
        }
        else{
            FCView::getAttribute(name, value, type);
        }
    }

    ArrayList<String> FCLine2D::getAttributeNames(){
        ArrayList<String> attributeNames = FCView::getAttributeNames();
        attributeNames.add(L"Close");
        attributeNames.add(L"LineWidth");
        attributeNames.add(L"Points");
        return attributeNames;
    }

    void FCLine2D::onPaint(FCPaint *paint, const FCRect& clipRect){
        if ((int)m_points.length() > 0) {
            ArrayList<String> strPoints = FCTran::split(m_points, L" ");
            int strPointsSize = strPoints.size();
            FCPoint *drawPoints = new FCPoint[strPointsSize];
            for (int i = 0; i < strPointsSize; i++) {
                ArrayList<String> subStrs = FCTran::split(strPoints.get(i), L",");
                FCPoint thisPoint;
                if ((int)subStrs.get(0).find(L"%") != -1) {
                    thisPoint.x = (int)(FCTran::strToDouble(FCTran::replace(subStrs.get(0), L"%", L"")) * getWidth() / 100);
                } else {
                    thisPoint.x = FCTran::strToInt(subStrs.get(0));
                }
                if ((int)subStrs.get(1).find(L"%") != -1) {
                    thisPoint.y = (int)(FCTran::strToDouble(FCTran::replace(subStrs.get(1), L"%", L"")) * getHeight() / 100);
                } else {
                    thisPoint.y = FCTran::strToInt(subStrs.get(1));
                }
                drawPoints[i] = thisPoint;
            }
            if (m_close) {
                paint->fillPolygon(getPaintingBackColor(), drawPoints, strPointsSize);
                paint->drawPolygon(getPaintingBorderColor(), m_lineWidth, 0, drawPoints, strPointsSize);
            } else {
                paint->drawPolyline(getPaintingBorderColor(), m_lineWidth, 0, drawPoints, strPointsSize);
            }
            delete[] drawPoints;
            drawPoints = 0;
        }
        callPaintEvents(L"onpaint", paint, clipRect);
    }

    void FCLine2D::onPaintBorder(FCPaint *paint, const FCRect& clipRect){
        callPaintEvents(L"onpaintborder", paint, clipRect);
    }

    void FCLine2D::setAttribute(const String& name, const String& value){
        if (name == L"close"){
            setClose(FCTran::strToBool(value));
        }
        else if(name == L"linewidth"){
            setLineWidth(FCTran::strToFloat(value));
        }
        else if(name == L"points"){
            setPoints(value);
        }
        else{
            FCView::setAttribute(name, value);
        }
    }
}
